package com.lt.service.impl;

import com.lt.entity.Login;
import com.lt.entity.Notice;
import com.lt.mapper.LoginMapper;
import com.lt.mapper.NoticeMapper;
import com.lt.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author :liteng
 * @Time : 2023/10/29 21:06
 */
@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {


    private final NoticeMapper noticeMapper;

    private final LoginMapper loginMapper;


    public NoticeServiceImpl(NoticeMapper noticeMapper, LoginMapper loginMapper) {
        this.noticeMapper = noticeMapper;
        this.loginMapper = loginMapper;
    }


    @Override
    public List<Notice> getNotices(String keyword, int dept, int startPage, int pageSize) {
        List<Notice> notices = noticeMapper.queryNotice(dept, keyword, startPage, pageSize);
        return notices;
//        return null;
    }

    @Override
    public List<Notice> getNotices(String keyword, int startPage, int pageSize) {
        return noticeMapper.queryNotice(0, keyword, startPage, pageSize);

    }

    @Override
    public int getNoticesNum(String keyword, int dept) {
        return noticeMapper.selectNoticeNum(dept,keyword);
    }

    @Override
    public void delete(Long id) {
        noticeMapper.deleteNotice(id);
    }

    @Override
    public Notice getOne(Long id) {
        return noticeMapper.selectOne(id);
    }

    @Override
    public Notice getToBeModifiedOne(Long noticeId) {
        return noticeMapper.selectToBeModifiedOne(noticeId);

    }

    @Override
    public Boolean updateNotice(Notice notice, Long num) {
        Login login = loginMapper.selectId(num);

        notice.setModifiedUserId(login.getId());
        notice.setModifiedTime(LocalDateTime.now());
        int i = noticeMapper.updateNotice(notice);
        return i == 1;
    }
}
